#include "FreeRTOS.h"
#include "task.h"
#include "stdio.h"

#include "led.h"
#include "beep.h"
#include "usart.h"
#include "adc.h"
#include "oled.h"
#include "bmp.h"
#include "key.h"
#include "time.h"
#include "my_delay.h"
#include "dht11.h"

char commend_buf[128] = {0};
int commend_num = 0;
u8 window_num = 0;

//TaskHandle_t oledWindow1_hanle;
//TaskHandle_t autoControl_hanle;
//TaskHandle_t keyControl_hanle;

void oledWindow1();
void oledWindow2();
void oledWindow3();
void autoControl();
void keyControl();

//OLED屏页面一
void oledWindow1()
{
	window_num = 1;
	OLED_ShowPicture(5,20,128,35,BMP3,1);
	OLED_Refresh();
	while(1)
	{
		keyControl();
		autoControl();
	}
}

//OLED屏页面二
void oledWindow2()
{
	window_num = 2;
	OLED_ShowChinese(12,2,0,16,1);//华
	OLED_ShowChinese(30,2,1,16,1);//清
	OLED_ShowChinese(48,2,2,16,1);//远
	OLED_ShowChinese(66,2,3,16,1);//见
	OLED_ShowChinese(84,2,4,16,1);//教
	OLED_ShowChinese(102,2,5,16,1);//育
	//LED1:X(开/关)
	OLED_ShowString(8,20,"LED1:",12,1);
	
	//LED2:X(开/关)
	OLED_ShowString(70,20,"LED2:",12,1);
	
	//LED3:X(开/关)
	OLED_ShowString(8,35,"LED3:",12,1);

	//蜂鸣器:X(开/关)
	OLED_ShowChinese(70,35,2,12,1);
	OLED_ShowChinese(82,35,3,12,1);
	OLED_ShowChinese(94,35,4,12,1);
	OLED_ShowString(106,35,":",12,1);
	
	//光照强度:xxx(三位数数字)
	OLED_ShowChinese(8,50,5,12,1);
	OLED_ShowChinese(20,50,6,12,1);
	OLED_ShowChinese(32,50,7,12,1);
	OLED_ShowChinese(44,50,8,12,1);
	OLED_ShowString(56,50,":",12,1);
	
	OLED_Refresh();
	while(1)
	{	
		//LED1
		if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_8) == 1)
		{
			OLED_ShowChinese(40,20,1,12,1);
		}
		else
		{
			OLED_ShowChinese(40,20,0,12,1);
		}
		//LED2
		if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_9) == 1)
		{
			OLED_ShowChinese(102,20,1,12,1);
		}
		else
		{
			OLED_ShowChinese(102,20,0,12,1);
		}
		//LED3
		if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_10) == 1)
		{
			OLED_ShowChinese(40,35,1,12,1);
		}
		else
		{
			OLED_ShowChinese(40,35,0,12,1);
		}
		//蜂鸣器
		if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10) == 1)
		{
			OLED_ShowChinese(113,35,0,12,1);
		}
		else
		{
			OLED_ShowChinese(113,35,1,12,1);
		}
		//光照强度--动态
		OLED_ShowNum(64,50,100-100*adcAverageValue()/4096,3,12,1);
		//
		keyControl();
		autoControl();
		OLED_Refresh();
	}
}

//OLED屏页面三
void oledWindow3()
{
	u8 humidity,temperature;
	window_num = 3;
	OLED_ShowChinese(12,2,0,16,1);//华
	OLED_ShowChinese(30,2,1,16,1);//清
	OLED_ShowChinese(48,2,2,16,1);//远
	OLED_ShowChinese(66,2,3,16,1);//见
	OLED_ShowChinese(84,2,4,16,1);//教
	OLED_ShowChinese(102,2,5,16,1);//育
	//---温度
	OLED_ShowChinese(8,35,9,12,1);
	OLED_ShowChinese(20,35,8,12,1);
	OLED_ShowString(32,35,":",12,1);
	//---湿度
	OLED_ShowChinese(70,35,10,12,1);
	OLED_ShowChinese(82,35,8,12,1);
	OLED_ShowString(94,35,":",12,1);

	while(1)
	{	
		//温湿度数据--动态
		if(dht11ReadData(&temperature, &humidity) == 1)
		{
			OLED_ShowNum(39,35,temperature,3,12,1);	//显示温度
			OLED_ShowNum(101,35,humidity,3,12,1);	//显示湿度
		}
		keyControl();
		autoControl();
		OLED_Refresh();
	}
}

//led3通过光照传感器自动控制
void autoControl()
{	
	u32 light = 100-100*adcAverageValue()/4096;
	if(light <= 50)
	{
		TIM_SetCompare2(TIM1, 1000);
		ledOpen(GPIO_Pin_10);
	}
	else if(light <= 80)
	{
		TIM_SetCompare2(TIM1, 300);
		ledOpen(GPIO_Pin_10);
	}
	else if(light <= 100)
	{
		TIM_SetCompare2(TIM1, 0);
		ledOpen(GPIO_Pin_10);
	}
}


//设备按键控制
void keyControl()
{
	u8 key1,key2;

	key1 = keyValue(GPIO_Pin_4);
	key2 = keyValue(GPIO_Pin_5);
	
	if(key1 == 1)
	{
		if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_8) == SET)
		{
			ledOpen(GPIO_Pin_8);
		}
		else
		{
			ledClose(GPIO_Pin_8);
		}
	}
	else if(key1 == 2)
	{
		OLED_Clear();
		switch(window_num)
		{
			case 1:
				oledWindow3();
				break;
			case 2:
				oledWindow1();
				break;
			case 3:
				oledWindow2();
				break;	
		}
	}
	
	
	if(key2 == 1)
	{
		if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_9) == SET)
		{
			ledOpen(GPIO_Pin_9);
		}
		else
		{
			ledClose(GPIO_Pin_9);
		}
	}
	else if(key2 == 2)
	{
		OLED_Clear();
		switch(window_num)
		{
			case 1:
				oledWindow2();
				break;
			case 2:
				oledWindow3();
				break;
			case 3:
				oledWindow1();
				break;	
		}
	}

	if(key4Value() == 1)
	{
		if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10) == SET)
		{
			beepClose();
		}
		else
		{
			beepOpen();
		}
	}	
}



int main(){
	
	//中断优先级分组
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);	
	tim7Init();//基本定时器7初始化--延时定时器
	tim6Init();//基本定时器6初始化--按键复用判断定时器

	//led1，2，3初始化
	ledInit(GPIO_Pin_8);
	ledInit(GPIO_Pin_9);
	ledInit(GPIO_Pin_10);	
	tim1Init();//高级定时器1初始化--用于led3灯的pwm设置
	
	//按键1，2，3，4初始化
	keyInit(GPIO_Pin_4);
	keyInit(GPIO_Pin_5);
	keyInit(GPIO_Pin_6);
	key4Init();
	
	beepInit();//蜂鸣器初始化
	OLED_Init();//OLED屏幕初始化
	usart1Init();//usart1初始化，用于输出调试信息
	usart2Init();//usart2初始化，用于与ESP12S连接实现无线通信
	adcInit();//adc初始化，用于获取光照传感器数据
	dht11Init();//dht11温湿度传感器初始化
	wifiInit();//wifi初始化	
	
	oledWindow1();	//OLED屏任务

//	xTaskCreate(oledWindow1,"oledWindow1",128,NULL,1,&oledWindow1_hanle);
//	xTaskCreate(autoControl,"autoControl",128,NULL,1,&autoControl_hanle);
//	xTaskCreate(keyControl,"keyControl",128,NULL,1,&keyControl_hanle);
//	vTaskStartScheduler();

}









//xTaskCreate(vTaskLED,"vTaskLED",128,NULL,1,&LEDHanle);
//xTaskCreate(vTaskBeep,"vTaskBeep",128,NULL,2,&BeepHanle);
//xTaskCreate(vTaskADC,"vTaskADC",256,NULL,2,&ADCHanle);
//
//vTaskStartScheduler();

//void vTaskLED(void * pvParameters)
//{
//	for(;;)
//	{
//		ledOpen(GPIO_Pin_8);
//		vTaskDelay(1000);
//		ledClose(GPIO_Pin_8);
//		vTaskDelay(1000);
////		vTaskDelete(LEDHanle);
//	}
//}
//
//void vTaskBeep(void * pvParameters)
//{
//	for(;;)
//	{
//		beepOpen();
//		vTaskDelay(500);
//		beepClose();
//		vTaskDelay(500);
//	}
//}
//void vTaskADC(void *pvParameters)
//{
//	for(;;)
//	{
//		printf("adc_data = %d\r\n",adcAverageValue());
//		vTaskDelay(2000);
//	}
//}


